package com.lh.lepay.config;


import com.lh.lepay.common.RedisChatMemoryStore;
import com.lh.lepay.service.ChatMemoryService;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.memory.chat.TokenWindowChatMemory;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ChatMemoryConfig {
    private RedisChatMemoryStore redisChatMemoryStore;

    public ChatMemoryConfig(RedisChatMemoryStore redisChatMemoryStore) {
        this.redisChatMemoryStore = redisChatMemoryStore;
    }

    @Bean
    public ChatMemoryService chatMemoryService(StreamingChatLanguageModel ollamaStreamingChatModel) {
        return AiServices.builder(ChatMemoryService.class)
                .streamingChatLanguageModel(ollamaStreamingChatModel)
                .chatMemoryProvider( memoryId ->
                        MessageWindowChatMemory.builder()
                                .maxMessages(15)
                                .chatMemoryStore(redisChatMemoryStore)
                                .id(memoryId)
                                .build()
                )
                .build();
    }

}
